<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var data = [
        { parentId: 0, id: 1, value: "1" },
        { parentId: 3, id: 2, value: "2" },
        { parentId: 0, id: 3, value: "3" },
        { parentId: 1, id: 4, value: "4" },
        { parentId: 1, id: 5, value: "5" }
      ];
      const convert = list => {
        const map = list.reduce((res, v) => {
          res[v.id] = v;
          return res;
        }, {});
        const res = [];
        for (let item of list) {
          if (item.parentId === 0) {
            res.push(item);
            continue;
          }
          if (item.parentId in map) {
            const parent = map[item.parentId];
            parent.children = parent.children || [];
            parent.children.push(item);
          }
        }
        return res;
        console.log(res);
      };
    </script>
  </body>
</html>
